#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "llist.h"
struct llist_node_st * llist_create()
{
	struct llist_node_st *new;
	new=malloc(sizeof(struct llist_node_st));
	if(new==NULL)
	{
		return NULL;
	}
	new->prev=new->next=new;
	return new;
}
int llist_insert(struct llist_node_st *head,struct student *stu,int mode)
{
	struct llist_node_st *newnode;
	newnode=malloc(sizeof(struct llist_node_st));
	newnode->stu=*stu;
	
	if(newnode==NULL)
	{
		return -1;
	}
	if(mode==LLIST_FORWARD)
	{
		newnode->prev=head;
		newnode->next=head->next;
	}
	else if(mode==LLIST_BACKWARD)
	{
		newnode->prev=head->prev;
		newnode->next=head;
	}
	newnode->prev->next=newnode;
	newnode->next->prev=newnode;
	return 0;
}
void llist_travel(struct llist_node_st *head)
{
	struct llist_node_st *cur;
	for(cur=head->next;cur!=head;cur=cur->next)
	{
		printf("id=%d\n",cur->stu.id);
		printf("name=%s\n",cur->stu.name);
		printf("math=%d\n",cur->stu.math);
	}

}

struct student * llist_find(struct llist_node_st *head,int id)
{
	struct llist_node_st *cur;
	for(cur=head->next;cur!=head;cur=cur->next)
	{
		if(cur->stu.id==id)
		{
			return &cur->stu;
		}
	}
	return NULL;

}
void llist_destroy(struct llist_node_st *head)
{
	struct llist_node_st *cur,*save;
	for(cur=head->next;cur!=head;cur=save)
	{
		save=cur->next;
		free(cur);
	}
	free(head);
}
int llist_delete(struct llist_node_st *head,int id)
{
	struct llist_node_st *cur;
	for(cur=head->next;cur!=head;cur=cur->next)
	{
		if(cur->stu.id==id)
		{
			cur->prev->next=cur->next;
			cur->next->prev=cur->prev;
			free(cur);
			
			return 0;
		}
	}
	return -1;
}







